```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Netty自定义协议编解码技术详解</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background: #2d3748;
            border-radius: 0.5rem;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .code-header {
            border-top-left-radius: 0.5rem;
            border-top-right-radius: 0.5rem;
            background: #1a202c;
        }
        .protocol-card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .protocol-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
        }
        .nav-link::after {
            content: '';
            display: block;
            width: 0;
            height: 2px;
            background: #667eea;
            transition: width .3s;
        }
        .nav-link:hover::after {
            width: 100%;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- 导航栏 -->
    <nav class="bg-white shadow-sm sticky top-0 z-50">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between h-16">
                <div class="flex items-center">
                    <span class="text-xl font-bold text-indigo-600">Netty技术栈</span>
                </div>
                <div class="hidden md:flex items-center space-x-8">
                    <a href="#intro" class="nav-link text-gray-700 hover:text-indigo-600">协议概述</a>
                    <a href="#encoder" class="nav-link text-gray-700 hover:text-indigo-600">编码器</a>
                    <a href="#decoder" class="nav-link text-gray-700 hover:text-indigo-600">解码器</a>
                    <a href="#pipeline" class="nav-link text-gray-700 hover:text-indigo-600">管道配置</a>
                    <a href="#example" class="nav-link text-gray-700 hover:text-indigo-600">完整示例</a>
                </div>
                <div class="md:hidden flex items-center">
                    <button class="text-gray-500 hover:text-gray-700 focus:outline-none">
                        <i class="fas fa-bars text-xl"></i>
                    </button>
                </div>
            </div>
        </div>
    </nav>

    <!-- Hero Section -->
    <header class="hero-gradient text-white py-20">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold leading-tight mb-4">Netty自定义协议编解码</h1>
                    <p class="text-xl opacity-90 mb-8">深入解析高性能网络通信中的自定义协议设计与实现</p>
                    <div class="flex space-x-4">
                        <a href="#intro" class="bg-white text-indigo-600 px-6 py-3 rounded-lg font-medium hover:bg-gray-100 transition duration-300">开始学习</a>
                        <a href="#example" class="bg-indigo-800 text-white px-6 py-3 rounded-lg font-medium hover:bg-indigo-900 transition duration-300">查看示例</a>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="w-full max-w-md">
                        <div class="mermaid">
                            graph LR
                            A[客户端] -->|发送字节流| B(解码器)
                            B --> C[消息对象]
                            C --> D[业务处理器]
                            D --> E[响应消息对象]
                            E --> F(编码器)
                            F -->|返回字节流| A
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <!-- 主要内容区 -->
    <main class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-16">
        <!-- 介绍部分 -->
        <section id="intro" class="mb-20">
            <div class="mb-12 text-center">
                <h2 class="text-3xl font-bold mb-4 text-gray-800">自定义协议基础</h2>
                <div class="w-20 h-1 bg-indigo-600 mx-auto"></div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-project-diagram text-indigo-500 mr-2"></i>
                        为什么需要自定义协议?
                    </h3>
                    <p class="text-gray-700 mb-6">
                        在网络通信中，协议定义了数据交换的格式和规则。自定义协议可以根据特定应用场景优化传输效率、安全性和扩展性。Netty提供了灵活的编解码机制，使开发者能够轻松实现高性能的自定义协议。
                    </p>
                    <div class="bg-blue-50 border-l-4 border-blue-500 p-4 mb-6">
                        <p class="text-blue-700">
                            <i class="fas fa-info-circle text-blue-500 mr-2"></i>
                            Netty内置了多种常见协议(如HTTP、WebSocket)的编解码器，但对于特殊需求，自定义协议往往是更好的选择。
                        </p>
                    </div>
                </div>
                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-code text-indigo-500 mr-2"></i>
                        协议基本结构
                    </h3>
                    <div class="protocol-card bg-white p-6 rounded-lg">
                        <div class="flex items-center mb-4">
                            <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-green-500"></div>
                        </div>
                        <div class="mb-4">
                            <div class="flex items-center justify-between mb-2">
                                <span class="font-medium">消息头 (Header)</span>
                                <span class="text-sm text-gray-500">5字节</span>
                            </div>
                            <div class="grid grid-cols-5 gap-2">
                                <div class="bg-indigo-100 p-2 rounded text-center text-sm">长度(4B)</div>
                                <div class="bg-indigo-100 p-2 rounded text-center text-sm">类型(1B)</div>
                                <div class="col-span-3 bg-gray-100 p-2 rounded text-center text-sm text-gray-500">未使用</div>
                            </div>
                        </div>
                        <div>
                            <div class="flex items-center justify-between mb-2">
                                <span class="font-medium">消息内容 (Content)</span>
                                <span class="text-sm text-gray-500">可变长度</span>
                            </div>
                            <div class="bg-green-100 p-4 rounded text-sm">
                                实际传输的数据内容，长度由Header中的长度字段指定
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-12">
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>
                        协议设计要点
                    </h3>
                    <div class="grid md:grid-cols-3 gap-6">
                        <div class="flex items-start">
                            <div class="bg-indigo-100 p-2 rounded-full mr-3">
                                <i class="fas fa-ruler-combined text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-medium mb-1">清晰的结构</h4>
                                <p class="text-gray-600 text-sm">明确定义各个字段的长度和位置，避免歧义</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="bg-indigo-100 p-2 rounded-full mr-3">
                                <i class="fas fa-shield-alt text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-medium mb-1">安全考虑</h4>
                                <p class="text-gray-600 text-sm">设计适当的校验机制防止恶意数据</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="bg-indigo-100 p-2 rounded-full mr-3">
                                <i class="fas fa-expand-arrows-alt text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-medium mb-1">扩展性</h4>
                                <p class="text-gray-600 text-sm">预留字段或使用TLV结构方便未来扩展</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- 编码器部分 -->
        <section id="encoder" class="mb-20">
            <div class="mb-12 text-center">
                <h2 class="text-3xl font-bold mb-4 text-gray-800">自定义编码器实现</h2>
                <div class="w-20 h-1 bg-indigo-600 mx-auto"></div>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-keyboard text-indigo-500 mr-2"></i>
                        编码器工作原理
                    </h3>
                    <p class="text-gray-700 mb-4">
                        编码器负责将高级消息对象转换为字节流，以便通过网络传输。在Netty中，通常继承<code class="bg-gray-100 px-1 rounded">MessageToByteEncoder</code>类实现自定义编码器。
                    </p>
                    <ul class="space-y-3 mb-6">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>将消息对象转换为协议定义的二进制格式</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>处理必要的字节序(大端/小端)问题</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>可以添加消息加密或压缩等附加功能</span>
                        </li>
                    </ul>
                    <div class="bg-yellow-50 border-l-4 border-yellow-500 p-4">
                        <p class="text-yellow-700">
                            <i class="fas fa-exclamation-triangle text-yellow-500 mr-2"></i>
                            编码器应确保线程安全，因为Netty可能在不同线程中调用编码方法。
                        </p>
                    </div>
                </div>
                <div>
                    <div class="code-block">
                        <div class="code-header px-4 py-2 flex items-center">
                            <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-green-500 mr-4"></div>
                            <span class="text-gray-300 text-sm">CustomProtocolEncoder.java</span>
                        </div>
                        <pre class="p-4 overflow-x-auto text-gray-200"><code>import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class CustomProtocolEncoder extends MessageToByteEncoder&lt;CustomMessage&gt; {

    @Override
    protected void encode(ChannelHandlerContext ctx, 
                         CustomMessage msg, 
                         ByteBuf out) throws Exception {
        // 写入消息长度(4字节)
        out.writeInt(msg.getLength());
        
        // 写入消息类型(1字节)
        out.writeByte(msg.getType());
        
        // 写入消息内容
        out.writeBytes(msg.getContent());
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-cogs text-indigo-500 mr-2"></i>
                        编码器实现细节
                    </h3>
                    <div class="grid md:grid-cols-3 gap-6">
                        <div class="border-l-4 border-indigo-500 pl-4">
                            <h4 class="font-medium mb-2 text-gray-700">ByteBuf操作</h4>
                            <p class="text-gray-600 text-sm">Netty使用ByteBuf代替Java NIO的ByteBuffer，提供了更灵活的读写操作和更好的性能。</p>
                        </div>
                        <div class="border-l-4 border-indigo-500 pl-4">
                            <h4 class="font-medium mb-2 text-gray-700">内存管理</h4>
                            <p class="text-gray-600 text-sm">Netty有高效的内存管理机制，编码器不需要显式释放ByteBuf。</p>
                        </div>
                        <div class="border-l-4 border-indigo-500 pl-4">
                            <h4 class="font-medium mb-2 text-gray-700">异常处理</h4>
                            <p class="text-gray-600 text-sm">编码过程中应妥善处理异常，避免导致整个管道失效。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- 解码器部分 -->
        <section id="decoder" class="mb-20">
            <div class="mb-12 text-center">
                <h2 class="text-3xl font-bold mb-4 text-gray-800">自定义解码器实现</h2>
                <div class="w-20 h-1 bg-indigo-600 mx-auto"></div>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="order-2 md:order-1">
                    <div class="code-block">
                        <div class="code-header px-4 py-2 flex items-center">
                            <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-green-500 mr-4"></div>
                            <span class="text-gray-300 text-sm">CustomProtocolDecoder.java</span>
                        </div>
                        <pre class="p-4 overflow-x-auto text-gray-200"><code>import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;

public class CustomProtocolDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, 
                         ByteBuf in, 
                         List&lt;Object&gt; out) throws Exception {
        // 检查是否有足够的字节
        if (in.readableBytes() &lt; 5) { // 4字节长度 + 1字节类型
            return;
        }
        
        // 标记读取位置
        in.markReaderIndex();
        
        // 读取消息长度
        int length = in.readInt();
        
        // 读取消息类型
        byte type = in.readByte();
        
        // 检查是否有足够字节读取内容
        if (in.readableBytes() &lt; length) {
            in.resetReaderIndex(); // 恢复读取位置
            return;
        }
        
        // 读取消息内容
        byte[] content = new byte[length];
        in.readBytes(content);
        
        // 创建消息对象并添加到输出列表
        out.add(new CustomMessage(length, type, content));
    }
}</code></pre>
                    </div>
                </div>
                <div class="order-1 md:order-2">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-laptop-code text-indigo-500 mr-2"></i>
                        解码器关键点
                    </h3>
                    <p class="text-gray-700 mb-4">
                        解码器负责将字节流解析为高级消息对象，是协议实现中最复杂的部分，需要处理TCP的粘包/拆包问题。
                    </p>
                    <div class="space-y-4 mb-6">
                        <div class="flex items-start">
                            <div class="bg-indigo-100 p-2 rounded-full mr-3 flex-shrink-0">
                                <i class="fas fa-bookmark text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-medium mb-1">标记与重置</h4>
                                <p class="text-gray-600 text-sm">使用<code class="bg-gray-100 px-1 rounded">markReaderIndex()</code>和<code class="bg-gray-100 px-1 rounded">resetReaderIndex()</code>在数据不足时恢复读取位置。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="bg-indigo-100 p-2 rounded-full mr-3 flex-shrink-0">
                                <i class="fas fa-shield-virus text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-medium mb-1">边界检查</h4>
                                <p class="text-gray-600 text-sm">始终检查可用字节数，防止<code class="bg-gray-100 px-1 rounded">IndexOutOfBoundsException</code>。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="bg-indigo-100 p-2 rounded-full mr-3 flex-shrink-0">
                                <i class="fas fa-project-diagram text-indigo-600"></i>
                            </div>
                            <div>
                                <h4 class="font-medium mb-1">状态管理</h4>
                                <p class="text-gray-600 text-sm">复杂的协议可能需要维护解码状态(如使用<code class="bg-gray-100 px-1 rounded">ReplayingDecoder</code>)。</p>
                            </div>
                        </div>
                    </div>
                    <div class="bg-blue-50 border-l-4 border-blue-500 p-4">
                        <p class="text-blue-700">
                            <i class="fas fa-info-circle text-blue-500 mr-2"></i>
                            对于可变长度协议，通常会先读取长度字段，然后根据长度值读取内容字段。
                        </p>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-exclamation-circle text-indigo-500 mr-2"></i>
                        常见问题与解决方案
                    </h3>
                    <div class="grid md:grid-cols-2 gap-6">
                        <div>
                            <h4 class="font-medium mb-2 text-gray-700">粘包/拆包问题</h4>
                            <p class="text-gray-600 text-sm mb-4">TCP是流式协议，消息可能被拆分或合并。解决方案包括:</p>
                            <ul class="space-y-2 text-sm text-gray-600">
                                <li class="flex items-start">
                                    <i class="fas fa-check text-green-500 mt-1 mr-2 text-xs"></i>
                                    <span>固定长度消息(不够灵活)</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-green-500 mt-1 mr-2 text-xs"></i>
                                    <span>分隔符(如换行符)</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-green-500 mt-1 mr-2 text-xs"></i>
                                    <span>长度字段(最常用)</span>
                                </li>
                            </ul>
                        </div>
                        <div>
                            <h4 class="font-medium mb-2 text-gray-700">性能优化</h4>
                            <p class="text-gray-600 text-sm mb-4">解码器性能直接影响吞吐量:</p>
                            <ul class="space-y-2 text-sm text-gray-600">
                                <li class="flex items-start">
                                    <i class="fas fa-check text-green-500 mt-1 mr-2 text-xs"></i>
                                    <span>减少对象创建(重用对象)</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-green-500 mt-1 mr-2 text-xs"></i>
                                    <span>使用<code class="bg-gray-100 px-1 rounded">ByteBuf</code>原生操作</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check text-green-500 mt-1 mr-2 text-xs"></i>
                                    <span>避免不必要的拷贝</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- 管道配置部分 -->
        <section id="pipeline" class="mb-20">
            <div class="mb-12 text-center">
                <h2 class="text-3xl font-bold mb-4 text-gray-800">管道(Pipeline)配置</h2>
                <div class="w-20 h-1 bg-indigo-600 mx-auto"></div>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-random text-indigo-500 mr-2"></i>
                        ChannelPipeline架构
                    </h3>
                    <p class="text-gray-700 mb-4">
                        Netty的ChannelPipeline是一组ChannelHandler的有序集合，处理入站和出站事件。数据流经pipeline中的各个handler，每个handler完成特定功能。
                    </p>
                    <div class="mermaid mb-6">
                        graph LR
                        A[客户端] -->|入站| B[解码器]
                        B --> C[业务处理器1]
                        C --> D[业务处理器2]
                        D -->|出站| E[编码器]
                        E --> A
                    </div>
                    <div class="bg-yellow-50 border-l-4 border-yellow-500 p-4">
                        <p class="text-yellow-700">
                            <i class="fas fa-exclamation-triangle text-yellow-500 mr-2"></i>
                            Handler的顺序很重要！解码器应该在业务处理器之前，编码器应该在业务处理器之后。
                        </p>
                    </div>
                </div>
                <div>
                    <div class="code-block">
                        <div class="code-header px-4 py-2 flex items-center">
                            <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-green-500 mr-4"></div>
                            <span class="text-gray-300 text-sm">CustomProtocolInitializer.java</span>
                        </div>
                        <pre class="p-4 overflow-x-auto text-gray-200"><code>import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

public class CustomProtocolInitializer extends ChannelInitializer&lt;SocketChannel&gt; {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        
        // 添加自定义解码器
        pipeline.addLast(new CustomProtocolDecoder());
        
        // 添加自定义编码器
        pipeline.addLast(new CustomProtocolEncoder());
        
        // 添加业务处理器
        pipeline.addLast(new CustomMessageHandler());
        
        // 可以添加更多处理器(如日志、监控等)
        // pipeline.addLast(new LoggingHandler());
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-12">
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-layer-group text-indigo-500 mr-2"></i>
                        处理器(Handler)生命周期
                    </h3>
                    <div class="grid md:grid-cols-3 gap-6">
                        <div>
                            <h4 class="font-medium mb-2 text-gray-700">添加处理器</h4>
                            <p class="text-gray-600 text-sm">通过<code class="bg-gray-100 px-1 rounded">pipeline.addLast()</code>等方法添加，支持名称和顺序指定。</p>
                        </div>
                        <div>
                            <h4 class="font-medium mb-2 text-gray-700">事件传播</h4>
                            <p class="text-gray-600 text-sm">调用<code class="bg-gray-100 px-1 rounded">ctx.fireChannelRead()</code>等方法将事件传递给下一个handler。</p>
                        </div>
                        <div>
                            <h4 class="font-medium mb-2 text-gray-700">异常处理</h4>
                            <p class="text-gray-600 text-sm">实现<code class="bg-gray-100 px-1 rounded">exceptionCaught()</code>方法处理异常，或传播给默认异常处理器。</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-indigo-50 rounded-lg p-6">
                <h3 class="text-xl font-semibold mb-4 text-indigo-800 flex items-center">
                    <i class="fas fa-lightbulb text-indigo-600 mr-2"></i>
                    最佳实践建议
                </h3>
                <div class="grid md:grid-cols-2 gap-6">
                    <div>
                        <h4 class="font-medium mb-2 text-indigo-700">处理器设计原则</h4>
                        <ul class="space-y-2 text-sm text-indigo-600">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>保持handler职责单一</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>避免阻塞操作，使用异步处理</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>合理利用@Sharable注解</span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="font-medium mb-2 text-indigo-700">性能优化技巧</h4>
                        <ul class="space-y-2 text-sm text-indigo-600">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>共享无状态handler实例</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>使用ExecutionHandler分离IO和业务线程</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>减少pipeline中的handler数量</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- 完整示例部分 -->
        <section id="example" class="mb-20">
            <div class="mb-12 text-center">
                <h2 class="text-3xl font-bold mb-4 text-gray-800">完整实现示例</h2>
                <div class="w-20 h-1 bg-indigo-600 mx-auto"></div>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-cubes text-indigo-500 mr-2"></i>
                        消息对象定义
                    </h3>
                    <div class="code-block">
                        <div class="code-header px-4 py-2 flex items-center">
                            <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-green-500 mr-4"></div>
                            <span class="text-gray-300 text-sm">CustomMessage.java</span>
                        </div>
                        <pre class="p-4 overflow-x-auto text-gray-200"><code>public class CustomMessage {
    private final int length;
    private final byte type;
    private final byte[] content;

    public CustomMessage(int length, byte type, byte[] content) {
        this.length = length;
        this.type = type;
        this.content = content;
    }

    public int getLength() {
        return length;
    }

    public byte getType() {
        return type;
    }

    public byte[] getContent() {
        return content;
    }

    @Override
    public String toString() {
        return "CustomMessage[length=" + length + 
               ", type=" + type + 
               ", content=" + new String(content) + "]";
    }
}</code></pre>
                    </div>
                </div>
                <div>
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-terminal text-indigo-500 mr-2"></i>
                        业务处理器
                    </h3>
                    <div class="code-block">
                        <div class="code-header px-4 py-2 flex items-center">
                            <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-green-500 mr-4"></div>
                            <span class="text-gray-300 text-sm">CustomMessageHandler.java</span>
                        </div>
                        <pre class="p-4 overflow-x-auto text-gray-200"><code>import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class CustomMessageHandler 
    extends SimpleChannelInboundHandler&lt;CustomMessage&gt; {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, 
                               CustomMessage msg) throws Exception {
        // 处理接收到的消息
        System.out.println("Received message: " + msg);
        
        // 可以构建响应消息
        byte[] responseContent = ("Echo: " + new String(msg.getContent()))
                                  .getBytes();
        CustomMessage response = new CustomMessage(
            responseContent.length, 
            (byte)1, 
            responseContent
        );
        
        // 写回响应
        ctx.writeAndFlush(response);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, 
                               Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                        <i class="fas fa-rocket text-indigo-500 mr-2"></i>
                        启动服务器与客户端
                    </h3>
                    <div class="grid md:grid-cols-2 gap-6">
                        <div>
                            <h4 class="font-medium mb-2 text-gray-700">服务器启动</h4>
                            <div class="code-block">
                                <div class="code-header px-4 py-2 flex items-center">
                                    <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                                    <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                                    <div class="w-3 h-3 rounded-full bg-green-500 mr-4"></div>
                                    <span class="text-gray-300 text-sm">Server.java</span>
                                </div>
                                <pre class="p-4 overflow-x-auto text-gray-200"><code>import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class Server {
    public static void main(String[] args) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .childHandler(new CustomProtocolInitializer());
            
            ChannelFuture f = b.bind(8080).sync();
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}</code></pre>
                            </div>
                        </div>
                        <div>
                            <h4 class="font-medium mb-2 text-gray-700">客户端启动</h4>
                            <div class="code-block">
                                <div class="code-header px-4 py-2 flex items-center">
                                    <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                                    <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                                    <div class="w-3 h-3 rounded-full bg-green-500 mr-4"></div>
                                    <span class="text-gray-300 text-sm">Client.java</span>
                                </div>
                                <pre class="p-4 overflow-x-auto text-gray-200"><code>import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Client {
    public static void main(String[] args) throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioSocketChannel.class)
             .handler(new CustomProtocolInitializer());
            
            Channel ch = b.connect("localhost", 8080).sync().channel();
            
            // 发送测试消息
            byte[] content = "Hello Netty".getBytes();
            CustomMessage msg = new CustomMessage(
                content.length, 
                (byte)1, 
                content
            );
            ch.writeAndFlush(msg);
            
            ch.closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- 总结部分 -->
        <section class="mb-20">
            <div class="bg-gradient-to-r from-indigo-500 to-purple-600 rounded-2xl p-8 text-white">
                <h2 class="text-3xl font-bold mb-6">总结与扩展</h2>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-clipboard-check mr-2"></i>
                            关键要点
                        </h3>
                        <ul class="space-y-3 opacity-90">
                            <li class="flex items-start">
                                <i class="fas fa-check mt-1 mr-2"></i>
                                <span>自定义协议设计应明确字段定义和边界处理</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check mt-1 mr-2"></i>
                                <span>编解码器需要正确处理TCP的粘包/拆包问题</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check mt-1 mr-2"></i>
                                <span>ChannelPipeline的handler顺序非常重要</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check mt-1 mr-2"></i>
                                <span>Netty的ByteBuf提供了高效的字节操作API</span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-arrow-up mr-2"></i>
                            进阶方向
                        </h3>
                        <ul class="space-y-3 opacity-90">
                            <li class="flex items-start">
                                <i class="fas fa-caret-right mt-1 mr-2"></i>
                                <span>使用Protocol Buffers或Thrift定义协议</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-caret-right mt-1 mr-2"></i>
                                <span>实现更复杂的协议状态机</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-caret-right mt-1 mr-2"></i>
                                <span>添加SSL/TLS加密支持</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-caret-right mt-1 mr-2"></i>
                                <span>集成压缩和心跳机制</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- 页脚 -->
    <footer class="bg-gray-900 text-gray-400 py-8">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col items-center">
                <div class="mb-4">
                    <span class="text-xl font-medium text-white">技术小馆</span>
                </div>
                <div class="mb-6">
                    <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-white transition duration-300">
                        <i class="fas fa-globe mr-2"></i>
                        http://www.yuque.com/jtostring
                    </a>
                </div>
                <div class="text-sm">
                    &copy; 2023 Netty技术栈. 保留所有权利.
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```